/*
 * GUID.java
 * @author Andy
 * Created on September 12, 2006, 4:44 PM
 */
package edu.byu.isys415.alm223;
import java.lang.*;
import java.net.*;
import javax.swing.JOptionPane;

public class GUID {
    
    private static long lasttime = 0;  //<--this can't be created in the method, as we'll need it after the method ceases
    private static long counter = 1;   // static means it's outside the class.
    /** Creates a new instance of GUID */
  private GUID() {
    // creates the GUID class
  }
  
  public static synchronized String generate() throws Exception {
        
        long now = System.currentTimeMillis();  //javadoc says it's a long, and static
                                //this gets the current time
        String originaltimehex = Long.toHexString(now);   //uses java.lang.long
        String nowhex = originaltimehex;
        char buffer = '0';
        
        while (nowhex.length() < 16 ) {
            nowhex = buffer + nowhex; // this adds the buffer '0' to the hex as needed.
        }

        String ip = java.net.InetAddress.getLocalHost().getHostAddress(); 
                                    //this gets the IP address of the host
        String[] ipArray = ip.split("\\.");
        
        String iphex1 = Long.toHexString(Long.parseLong(ipArray[0]));
        String iphex2 = Long.toHexString(Long.parseLong(ipArray[1]));
        String iphex3 = Long.toHexString(Long.parseLong(ipArray[2]));
        String iphex4 = Long.toHexString(Long.parseLong(ipArray[3])); 
                            // we now have the IP address broken into 4 strings.
        while (iphex1.length() < 2 ) {  //these check to make sure each segment
            iphex1 = buffer + iphex1;   //of the IP hex has two characters.
        }
        while (iphex2.length() < 2 ) {
            iphex2 = buffer + iphex2;
        }
        while (iphex3.length() < 2 ) {
            iphex3 = buffer + iphex3;
        }
        while (iphex4.length() < 2 ) {
            iphex4 = buffer + iphex4;
        }
        String iphex = iphex1 + iphex2 + iphex3 + iphex4;
                
        if (now != lasttime){  //i had to do this "if" statement first, or the
         counter = 1;         //else statement later on would mess things up.
        }
        if (now == lasttime){
            counter++;
        }else lasttime = now;
        
        String count = Long.toHexString(counter);
            //System.out.println(count); //uncomment this see the counter in the printout.
        while (count.length() < 6 ) {
            count = buffer + count;
        }
        String fullhex = nowhex + count + iphex;
        
        if (counter > 16777214){
            Thread.sleep(5); // I can't use "1" millisecond since Java might ignore it'
            counter = 1;
        }
                       
    return fullhex;  //returns a complete GUID to the Main class as a STRING.
  }//end generate
  
  public static int response(){  //Used to query the user how many GUIDs he wants to create.
	String a = JOptionPane.showInputDialog("How many GUIDs do you want to generate?");
         int limit = Integer.parseInt(a);
	return limit;
  } //end response
} //end class GUID